/**
 * 
 */
package co.gov.medellin.sdep.sisdep.regulacion.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.regulacion.exception.EventoRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaCritValoracionException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaEvalPartException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.FeriaEvalPartRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.EventoMessageDescriptor;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.FeriaEvalPartMessageDescriptor;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.FeriaCritValoracion;
import co.gov.medellin.sdep.sisdep.model.FeriaEvalPart;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IFeriaEvalPartDAOT;

/**
 * @author Usuario
 *
 */
public class FeriaEvalPartServiceImpl extends BaseService implements IFeriaEvalPartService {

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                 init()
	//----------------------------------------------------------------------

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;
	/** */
	private IFeriaEvalPartDAOT feriaEvalPartDAO;

	/**
	 * 
	 */
	public FeriaEvalPartServiceImpl() {
	}

	//----------------------------------------------------------------------
	//--- propiedades y constructores                                  end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                     init()
	//----------------------------------------------------------------------

	/**
	 * 
	 */
	public void insert(FeriaEvalPart feriaEvalPart) throws FeriaEvalPartException {
		try{
			feriaEvalPart.setUsuarioCrea(getPrincipalUser());
			feriaEvalPart.setFechaCrea(new Date());
			getFeriaEvalPartDAO().insert(feriaEvalPart);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new FeriaEvalPartException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new FeriaEvalPartException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void update(FeriaEvalPart feriaEvalPart) throws FeriaEvalPartException {
		try{
			feriaEvalPart.setUsuarioModifica(getPrincipalUser());
			feriaEvalPart.setFechaModifica(new Date());
			getFeriaEvalPartDAO().update(feriaEvalPart);
		}catch (Exception e) {
			Object [] infoError = {
			};
			if(e.getCause().toString().indexOf("UNIQUE KEY") >= 0){
				throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			}else{
				throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/**
	 * 
	 */
	public final void delete(FeriaEvalPart feriaEvalPart) throws FeriaEvalPartException {
		try {
			feriaEvalPart.setUsuarioModifica(getPrincipalUser());
			feriaEvalPart.setFechaModifica(new Date());
			getFeriaEvalPartDAO().delete(feriaEvalPart);
		}catch (Exception e) {
			Object [] infoError = {
			};
			throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List load(int start,int max, FeriaEvalPart feriaEvalPart) throws FeriaEvalPartException {
		try{
			//return getFeriaEvalPartDAO().getAllFeriaEvalPartPaginated(start, max, criteria);
			return getFeriaEvalPartDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/**
	 * @return 
	 * 
	 */
	public final List loadByIdFeria(int start,int max, FeriaEvalPart feriaEvalPart) throws FeriaEvalPartException {
		try{
			//return getFeriaEvalPartDAO().getAllFeriaEvalPartPaginated(start, max, criteria);
			return getFeriaEvalPartDAO().loadByIdFeria(feriaEvalPart);
		}catch (Exception e) {
			Object [] infoError = null;
			throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
	/**
	 * 
	 */
	public final FeriaEvalPart get(long id) throws FeriaEvalPartException {
		try{
			return getFeriaEvalPartDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new FeriaEvalPartRuntimeException(new FeriaEvalPartMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/**
	 * 
	 */
	public FeriaEvalPart getFeriaEvalPartRelatedBy(long idSolicitud)
			throws FeriaEvalPartException {
		try {
			FeriaEvalPart criteria = new FeriaEvalPart();
			criteria.setIdFeriaArtesanal(new Long(idSolicitud));
			return getFeriaEvalPartDAO().findByCriteria(criteria);
		} catch (Exception e) {
			Object[] infoError = { new Long(idSolicitud) };
			throw new EventoRuntimeException(new EventoMessageDescriptor(
					MessageDescriptorConstant.GET), infoError, e);
		}
	}
	
	//----------------------------------------------------------------------
	//--- implementacion de metodos para acciones                      end()
	//----------------------------------------------------------------------

	//----------------------------------------------------------------------
	//--- getters and setters                                         init()
	//----------------------------------------------------------------------

	/**
	 * @author Usuario
	 *
	 */
	public IFeriaEvalPartDAOT getFeriaEvalPartDAO() {
		return feriaEvalPartDAO;
	}

	/**
	 * @author Usuario
	 *
	 */
	public void setFeriaEvalPartDAO(IFeriaEvalPartDAOT feriaEvalPartDAO) {
		this.feriaEvalPartDAO = feriaEvalPartDAO;
	}

	//----------------------------------------------------------------------
	//--- getters and setters                                          end()
	//----------------------------------------------------------------------

}
